﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccesLayer
{

   public  class ProjectsDataAccesLayer
    {

       public void AddNewProject(object project)
       {
           if (project == null || !(project is DataAccesLayer.Project)) return;
           if (((DataAccesLayer.Project)project).ProjectId == 0) return;
           eQualDataContext DataContext = new eQualDataContext();
           if (!(DataContext.Projects.Contains(project as DataAccesLayer.Project)))
               DataContext.Projects.InsertOnSubmit(project as DataAccesLayer.Project);
           DataContext.SubmitChanges();
       }

        public void DeleteProject(object project){
        }

        internal void DeleteProject(System .Collections .IList projectsList )
        {
        }

        public System.Collections.IEnumerable GetProjects()
        {
            eQualDataContext DataContext = new eQualDataContext();
            return DataContext.Projects.ToList();
        }

        public System.Collections.IEnumerable GetProjectTypes()
        {
            eQualDataContext DataContext = new eQualDataContext();
            return DataContext.ProjectTypes.ToList();
        }

        public System.Collections.IEnumerable GetProjectsStatusTypes()
        {
            eQualDataContext DataContext = new eQualDataContext();
            return DataContext.ProjectStatusTypes.ToList();
        }

        public Employee GetProjectOwner(Project project)
        {
            return project.Employee;
        }

        public System.Collections.IEnumerable GetTasks()
        {
            return new eQualDataContext().Tasks.ToList();
        }

        public IEnumerable<Task> GetTasks(object ActiveProject)
        {
            Project activeProject = ActiveProject as Project;

            eQualDataContext DataContext = new eQualDataContext();

            var ProjectTasks = from t in DataContext.Tasks
                               where t.Project == activeProject.ProjectId
                               select t;

            return ProjectTasks.ToList();
        }

        public void AddNewTaskToProject(object ActiveProject, object ActiveItem)
        {     
            DataAccesLayer.eQualDataContext DataContext = new eQualDataContext();

            Task _task = ActiveItem as Task;
            Project _project = ActiveProject as Project;

            if (!DataContext.Tasks.Contains(_task)) 
            {
                _task.Project = _project.ProjectId;
                DataContext.Tasks.InsertOnSubmit(_task);


                DataContext.SubmitChanges();
            }
        }

        public System.Collections.IEnumerable  GetProjectOwnToEmployee(Employee employee)
        {
            DataAccesLayer.eQualDataContext DataContext = new eQualDataContext();

            var projectOwnes = from p in DataContext.Projects
                               where p.ProjectOwnerId == employee.id
                               select p;

            return projectOwnes.ToList();
        }

        public IEnumerable<QualityEvent> GetQualityEvents()
        {
            return new eQualDataContext().QualityEvents.ToList();
        }

        public System.Collections.IEnumerable GetQualityEventsTypes()
        {
            return new eQualDataContext().QualityEventTypes.ToList();
        }

        public QualityEventType  GetQualityEventsType(string EventTypeName)
        {
            return new eQualDataContext().QualityEventTypes.SingleOrDefault(t => t.QualityEventType1 == EventTypeName);
        }

        public IEnumerable<QualityEvent> GetQualityEvents(QualityEventType QualityEventType)
        {
            eQualDataContext DataContext = new eQualDataContext();

            var qualityEvents = from qe in DataContext.QualityEvents
                                where qe.QualityType  == QualityEventType.QualityEventType1 
                                select qe;

            return qualityEvents.ToList();
        }

        public void AddNewQualityEventWithType(object qEvent, QualityEventType SelectedQualityEventType)
        {
            QualityEvent _qEvent = qEvent as QualityEvent;
            _qEvent.QualityType = SelectedQualityEventType.QualityEventType1 ;

            AddNewQualityEvent(_qEvent);
        }

        public void AddNewQualityEvent(object qEvent)
        {
            if (QualityEventExist(qEvent as QualityEvent)) return;

            QualityEvent _qEvent = qEvent as QualityEvent;

            eQualDataContext DataContext = new eQualDataContext();
            DataContext.QualityEvents.InsertOnSubmit(_qEvent);
            DataContext.SubmitChanges();
        }

        private bool QualityEventExist(QualityEvent qualityEvent)
        {
            eQualDataContext DataContext = new eQualDataContext();
            return DataContext.QualityEvents.SingleOrDefault(qe => qe.QualityEventId == qualityEvent.QualityEventId) != null;
        }

        public IEnumerable<Project> GetProjectsForQualityEvent(object  _qualityEvent)
        {
            QualityEvent qualityEvent = _qualityEvent as QualityEvent;

            eQualDataContext DataContext = new eQualDataContext();

            var projects = from p in DataContext.Projects
                           from e in DataContext.QualityEvents
                           from ea in DataContext.QualityEventActions
                           where p.ProjectId == ea.ProjectId
                           where e.QualityEventId == qualityEvent.QualityEventId
                           where e.QualityEventId == ea.QualityEventId
                           select p;

            return projects.ToList();
        }

        public void AddNewProjectToQualityEvent(object project, object qualityEvent)
        {
            if (ProjectToQualityEventExist(project, qualityEvent)) return;
            Project _project = project as Project;
            QualityEvent _qualityEvent = qualityEvent as QualityEvent;

            eQualDataContext DataContext = new eQualDataContext();
            QualityEventAction EventAction = new QualityEventAction();
            EventAction.ProjectId = _project.ProjectId;
            EventAction.QualityEventId = _qualityEvent.QualityEventId;

            DataContext.QualityEventActions.InsertOnSubmit(EventAction);
            DataContext.SubmitChanges();

        }

        protected virtual bool ProjectToQualityEventExist(object project, object qualityEvent)
        {
            Project _project = project as Project;
            QualityEvent _qualityEvent = qualityEvent as QualityEvent;

            eQualDataContext DataContext = new eQualDataContext();

            return DataContext.QualityEventActions.SingleOrDefault(qa => qa.QualityEventId == _qualityEvent.QualityEventId && qa.ProjectId == _project.ProjectId) != null;
        }



        public IEnumerable<Project> GetProjectsFromReview(object review)
        {
            Review _review = review as Review;

            eQualDataContext DataContext = new eQualDataContext();

            var projects = from p in DataContext.Projects
                           from rp in DataContext .ReviewProjects 
                           where p.ProjectId == rp.ProjectId 
                           where rp.ReviewId == _review .ReviewId 
                           select p;

            return projects.ToList();
        }

        public void AddNewProjectToReview(object project,object review)
        {
   
            Project _project = project as Project;
            Review  _review = review  as Review ;

         if (ProjectToReview(_project, _review)) return;

            eQualDataContext DataContext = new eQualDataContext();

            ReviewProject reviewproject = new ReviewProject();
            reviewproject.ReviewId = _review.ReviewId;
            reviewproject.ProjectId = _project.ProjectId;

            DataContext.ReviewProjects.InsertOnSubmit(reviewproject);

            DataContext.SubmitChanges();
        }

        protected bool ProjectToReview(Project project, Review review)
        {
            eQualDataContext DataContext = new eQualDataContext();
            return DataContext.ReviewProjects .SingleOrDefault(rp => rp.ProjectId  == project .ProjectId && rp.ReviewId == review .ReviewId ) != null;
        }

        public IEnumerable<Project> GetProjectForProposal(object ActiveItem)
        {
            throw new NotImplementedException();
        }

        public System.Collections.IEnumerable GetAudits()
        {
           return new eQualDataContext().Audits.ToList();
        }
    }
}
